﻿Imports System
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UI

Public Class JL200

    Public PAR_LCover_Ang As String
    Public PAR_LCover_LPos As String
    Public PAR_LZC_d_1 As String
    Public PAR_LZC_d_2 As String
    Public PAR_Main_D As String
    Public PAR_Main_L As String
    Public PAR_Main_T As String
    Public PAR_BZ_Count As String
    Public PAR_BZ_Dis As String
    Public PAR_BZ_RPos As String
    Public PAR_BZ_SDis As String
    Public PAR_RCover_RPos As String
    Public PAR_RZC_d_1 As String
    Public PAR_RZC_d_2 As String
    Public PAR_RZC_d_3 As String
    Public PAR_ZC_AZK_D As String

    Private model_type As String = "new"
    Private JL200_TEMP_DIR As String
    Private oWorkPart As Part

    Public Sub New()

        JL200_TEMP_DIR = TEMP_DIR & Format(Now(), "yyyyMMddHHmmss") & "\"
        JL200_MAIN_ASM = JL200_TEMP_DIR & "200-01-01-00-M3.prt"

    End Sub

    Function step_1(Optional ByVal bNew As Boolean = False) As Integer

        If bNew Then
            My.Computer.FileSystem.CopyDirectory(JL200_DIR, JL200_TEMP_DIR, True)
            oWorkPart = load_file(JL200_MAIN_ASM)
            'set_view(True)
        Else
            oWorkPart = get_session()
        End If

        Return 1
    End Function

    Function step_2() As Integer

        If PAR_BZ_Dis <> GetExp("BZ_Dis") Then

            SetExp("BZ_Count", "1")
            update()

            SetExp("BZ_Dis", PAR_BZ_Dis)
            update()

            SetExp("BZ_Count", PAR_BZ_Count)
            update()
        Else
            SetExp("BZ_Count", PAR_BZ_Count)
            update()
        End If

        Return 2
    End Function

    Function step_3() As Integer

        SetExp("LZC_d_2", PAR_LZC_d_2)
        SetExp("RZC_d_2", PAR_RZC_d_2)

        update()

        Return 3
    End Function

    Function step_4(ByVal sSaveTo As String) As Integer
        ' ----------------------------------------------
        '   菜单：文件->关闭->Save and Close
        ' ----------------------------------------------
        Dim partSaveStatus2 As PartSaveStatus
        partSaveStatus2 = oWorkPart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.True)

        oWorkPart = Nothing
        partSaveStatus2.Dispose()

        My.Computer.FileSystem.CopyDirectory(JL200_TEMP_DIR, sSaveTo, True)
        'My.Computer.FileSystem.DeleteDirectory(JL200_TEMP_DIR, FileIO.DeleteDirectoryOption.DeleteAllContents)

        load_file(sSaveTo & "\200-01-01-00-M3.prt")

        Return 4
    End Function

    Private Function load_file(ByVal sFile As String) As Part

        Dim theSession As Session = Session.GetSession()

        theSession.Parts.LoadOptions.UsePartialLoading = False
        theSession.Parts.LoadOptions.Save(Nothing)

        Dim basePart1 As BasePart
        Dim PLS As PartLoadStatus
        basePart1 = theSession.Parts.OpenBaseDisplay(sFile, PLS)
        PLS.Dispose()

        Return basePart1

    End Function

    Private Function get_session() As Part

        Dim theSession As Session = Session.GetSession()
        Dim basePart1 As Part = theSession.Parts.Work

        Return basePart1

    End Function

    Sub set_view(Optional ByVal zoom As Boolean = False)

        ' ----------------------------------------------
        '   菜单：定向视图->前视图
        ' ----------------------------------------------
        oWorkPart.ModelingViews.WorkView.Orient(NXOpen.View.Canned.Front, NXOpen.View.ScaleAdjustment.Fit)

        ' ----------------------------------------------
        '   菜单：渲染样式->静态线框
        ' ----------------------------------------------
        oWorkPart.ModelingViews.WorkView.RenderingStyle = NXOpen.View.RenderingStyleType.StaticWireframe

        If zoom Then
            ' ----------------------------------------------
            '   菜单：缩放
            ' ----------------------------------------------
            Dim corner1_1 As Point3d = New Point3d(25, 420, 0.0)
            Dim corner2_1 As Point3d = New Point3d(1210, -356, 0.0)
            oWorkPart.ModelingViews.WorkView.ZoomByRectangle(corner1_1, corner2_1)
        End If

    End Sub

    Sub reset_view()

        ' ----------------------------------------------
        '   菜单：定向视图->前视图
        ' ----------------------------------------------
        oWorkPart.ModelingViews.WorkView.Orient(NXOpen.View.Canned.Isometric, NXOpen.View.ScaleAdjustment.Fit)

        ' ----------------------------------------------
        '   菜单：渲染样式->静态线框
        ' ----------------------------------------------
        oWorkPart.ModelingViews.WorkView.RenderingStyle = NXOpen.View.RenderingStyleType.ShadedWithEdges

    End Sub
    Function GetExp(ByVal sExpression As String) As String
        Dim expression1 As Expression = CType(oWorkPart.Expressions.FindObject(sExpression), Expression)
        Return expression1.RightHandSide
    End Function

    Function SetExp(ByVal sExpression As String, ByVal sValue As String) As Boolean
        Dim expression1 As Expression = CType(oWorkPart.Expressions.FindObject(sExpression), Expression)
        oWorkPart.Expressions.Edit(expression1, sValue)
        Return True
    End Function

    Public Sub update()
        Dim theSession As Session = Session.GetSession()

        Dim markId2 As Session.UndoMarkId
        markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Update Expression Data")

        Dim nErrs1 As Integer
        nErrs1 = theSession.UpdateManager.DoUpdate(markId2)

        theSession.DeleteUndoMark(markId2, "Update Expression Data")
    End Sub


End Class

Public Class FolderBrowser
    Inherits System.Windows.Forms.Design.FolderNameEditor
    Public Shared Function ShowDialog() As String
        Dim fb As New FolderBrowser()
        '对话框的title
        fb.Description = "请选择一个目录"
        '默认是系统的桌面目录
        fb.StartLocation = Design.FolderNameEditor.FolderBrowserFolder.Desktop
        '对话框的样式
        fb.Style = Design.FolderNameEditor.FolderBrowserStyles.ShowTextBox
        fb.ShowDialog()
        Return fb.DirectoryPath
    End Function
End Class